package com.news.dao.impl;


import com.news.dao.CommentsDao;
import com.news.pojo.Comments;
import com.news.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CommentsDaoImpl implements CommentsDao {

    /**
     * 新增评论
     *
     * @param
     * @return
     */
   /* public static void main(String[] args) {
        CommentsDao commentsDao = new CommentsDaoImpl();
        Comments comments = new Comments();
        Date date = new Date();
        //comments.setCdate("2021-06-03");
        comments.setCip("测试测试");
        comments.setCauthor("测试测试");
        comments.setCcontent("12321234321");
        comments.setCnid(1);
        commentsDao.addComments(comments);
    }*/
    @Override
    public int addComments(Comments comments) {
        String sql = "INSERT INTO comments(cnid,ccontent,cdate,cip,cauthor) " +
                " VALUES(?,?,?,?,?);";
        Object [] params = {comments.getCnid(),comments.getCcontent(),comments.getCdate()
                ,comments.getCip(),comments.getCauthor()};
        return DBUtil.update(sql,params);
    }

    /**
     * 查询所有评论
     *
     * @return
     */
    @Override
    public List<Comments> getAll() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Comments> list = new ArrayList<Comments>();
        try{
            //1.获取连接对象
            conn = DBUtil.getConnection();
            //2.执行sql
            String sql = "SELECT cid,cnid,ccontent,cdate,cip,cauthor FROM comments;";
            System.out.println(sql);
            //3.获取预处理对象
            pstmt = conn.prepareStatement(sql);
            //5.处理结果集
            rs = pstmt.executeQuery();
            while(rs.next()){
                Comments comments = new Comments();
                comments.setCid(rs.getInt("cid"));
                comments.setCnid(rs.getInt("cnid"));
                comments.setCcontent(rs.getString("ccontent"));
                comments.setCdate(rs.getTimestamp("cdate"));
                comments.setCip(rs.getString("cip"));
                comments.setCauthor(rs.getString("cauthor"));
                list.add(comments);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.closeAll(conn, pstmt, rs);
        }
        return list;
    }

    /**
     * 通过新闻id查询评论
     *
     * @param
     * @return
     */
    @Override
    public List<Comments> getAllByNid(int nid) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Comments> list = new ArrayList<Comments>();
        try{
            //1.获取连接对象
            conn = DBUtil.getConnection();
            //2.执行sql
            String sql = "SELECT cid,cnid,ccontent,cdate,cip,cauthor FROM comments WHERE cnid =?;";
            System.out.println(sql);
            //3.获取预处理对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,nid);
            //5.处理结果集
            rs = pstmt.executeQuery();
            while(rs.next()){
                Comments comments = new Comments();
                comments.setCid(rs.getInt("cid"));
                comments.setCnid(rs.getInt("cnid"));
                comments.setCcontent(rs.getString("ccontent"));
                comments.setCdate(rs.getTimestamp("cdate"));
                comments.setCip(rs.getString("cip"));
                comments.setCauthor(rs.getString("cauthor"));
                list.add(comments);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.closeAll(conn, pstmt, rs);
        }
        return list;
    }
    }
